#!/usr/bin/python2.7
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from third_party.active_state.recipes.autoassign import autoassign
import django.template
'''
Created on Dec 9, 2011

@author: ibrahimelfar
'''

class UtteranceToSnippet:
    @autoassign
    def __init__(self, utternace, snippet_name): pass

    def __repr__(self):
        return ''.join(['{self.__class__}(',
                        'utterance="{self.utterance}", ',
                        'snippet_name="{self.snippet_name}")'
                        ]).format(self = self)

class UtteranceToSnippetPattern:
    @autoassign
    def __init__(self, synonyms, snippet_name_pattern, *arguments, **keyword_arguments):
        self.context = dict(keyword_arguments.items())
        
    def __repr__(self):
        return '''{self.__class__}(synonyms='{self.synonym_templates}', snippet_name='{self.snippet_name_templates}', context = '{self.context}')'''.format(self = self)

    def generate_terms(self):
        return ['test term change me']

class Template:
    @autoassign
    def __init__(self, name, value): pass

    def __repr__(self):
        return '''{self.__class__}(name='{self.name}', value='{self.value}')'''.format(self = self)

class SnippetPattern:
    @autoassign
    def __init__(self, name, value, *arguments, **keyword_arguments):
        self.context = dict(keyword_arguments.items())

    def __repr__(self):
        return '''{self.__class__}(name='{self.name}', value='{self.value}', context='{self.context}')'''.format(self = self)

    def generate_templates(self):
        if len(self.context) == 0 or len(self.context.itervalues().next()) == 0:
            return [Template(self.name, self.value)]
        
        length = len(self.context.itervalues().next())
        for key in self.context:
            if len(self.context[key]) != length:
                raise Exception('{0} has {1} elements. Expected {2}.'.format(key, len(self.context[key]), length))

        templates = []
        for i in range(len(self.context.itervalues().next())):
            rendering_context = django.template.Context(dict(zip(self.context.keys(),
                                                                 map(lambda l: l[i], self.context.values()))))
            # TODO put in a global setting
            # TODO custom escaping
            rendering_context['break'] = '\n'
            rendering_context['tab'] = '\t'
            
            templates.append(Template(
                                      django.template.Template(self.name).render(rendering_context),
                                      django.template.Template(self.value).render(rendering_context)))
            print 'generated template ' + str(templates[len(templates) - 1])
        return templates

language_modules = {}
def import_language_modules(user_settings):
    for language in user_settings.languages:
        language_modules[language] = __import__('listen.languages.' + language, fromlist = ['listen.languages'])

environment_modules = {}
def import_environment_modules(user_settings):
    for environment in user_settings.environments:
        environment_modules[environment] = __import__('listen.environments.' + environment, fromlist = ['listen.environments'])

speech_modules = {}
def import_speech_modules(user_settings):
    for speech_engine in user_settings.speech_engines:
        speech_modules[speech_engine] = __import__('listen.speech.' + speech_engine, fromlist = ['listen.speech'])
